package cn.youqu.happy.module.youqu.controller.app.userArrange;

import cn.youqu.happy.framework.common.pojo.CommonResult;
import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.security.core.annotations.PreAuthenticated;
import cn.youqu.happy.module.youqu.controller.admin.userArrange.vo.UserArrangeCreateReqVO;
import cn.youqu.happy.module.youqu.controller.admin.userArrange.vo.UserArrangePageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userArrange.vo.AppUserArrangeCreateReqVO;
import cn.youqu.happy.module.youqu.controller.app.userArrange.vo.AppUserArrangePageReqVO;
import cn.youqu.happy.module.youqu.controller.app.userArrange.vo.AppUserArrangeRespVO;
import cn.youqu.happy.module.youqu.convert.userArrange.UserArrangeConvert;
import cn.youqu.happy.module.youqu.dal.dataobject.userArrange.UserArrangeDO;
import cn.youqu.happy.module.youqu.service.userArrange.UserArrangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.youqu.happy.framework.common.pojo.CommonResult.success;
import static cn.youqu.happy.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 用户计划")
@RestController
@RequestMapping("/youqu/user-arrange")
@Validated
public class AppUserArrangeController {

    @Resource
    private UserArrangeService userArrangeService;

    @PostMapping("/create")
    @ApiOperation("创建用户计划")
    @PreAuthenticated

    public CommonResult<Integer> createUserArrange(@Valid @RequestBody AppUserArrangeCreateReqVO createReqVO) {
        UserArrangeCreateReqVO convert = UserArrangeConvert.INSTANCE.convert(createReqVO);
        Long loginUserId = getLoginUserId();
        convert.setUid(loginUserId);
        return success(userArrangeService.createUserArrange(convert));
    }
    //在活动状态编辑中申请
//    @PutMapping("/update")
//    @ApiOperation("更新用户计划")
//    @PreAuthenticated
//
//    public CommonResult<Boolean> updateUserArrange(@Valid @RequestBody AppUserArrangeUpdateReqVO updateReqVO) {
//        Long loginUserId = getLoginUserId();
//        userArrangeService.updateUserArrange(updateReqVO, loginUserId);
//        return success(true);
//    }
//在活动状态编辑中申请
//    @PostMapping("/cancel")
//    @ApiOperation("取消用户计划")
//    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
//    @PreAuthenticated
//
//    public CommonResult<Boolean> cancelUserArrange(@RequestParam("id") Integer id) {
//        Long loginUserId = getLoginUserId();
//        return success(userArrangeService.cancelUserArrange(id, loginUserId));
//    }


    @GetMapping("/get")
    @ApiOperation("获得用户计划")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthenticated

    public CommonResult<AppUserArrangeRespVO> getUserArrange(@RequestParam("id") Integer id) {
        Long loginUserId = getLoginUserId();
        UserArrangeDO userArrange = userArrangeService.getUserArrange(id, loginUserId);
        return success(UserArrangeConvert.INSTANCE.convert0(userArrange));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户计划列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthenticated

    public CommonResult<List<AppUserArrangeRespVO>> getUserArrangeList(@RequestParam("ids") Collection<Integer> ids) {
        Long loginUserId = getLoginUserId();
        List<UserArrangeDO> list = userArrangeService.getUserArrangeList(ids, loginUserId);
        return success(UserArrangeConvert.INSTANCE.convertList0(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户计划分页")
    @PreAuthenticated

    public CommonResult<PageResult<AppUserArrangeRespVO>> getUserArrangePage(@Valid AppUserArrangePageReqVO pageVO) {
        UserArrangePageReqVO convert = UserArrangeConvert.INSTANCE.convert(pageVO);
        Long loginUserId = getLoginUserId();
        PageResult<UserArrangeDO> pageResult = userArrangeService.getUserArrangePage(convert, loginUserId);
        return success(UserArrangeConvert.INSTANCE.convertPage0(pageResult));

    }


}
